package com.servlet;

import com.beans.AdminInfo;
import com.beans.PageInfo;
import com.dao.AdminDao;
import com.jdbc.Des;
import com.jdbc.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
    private AdminDao adminDao=new AdminDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String flag=request.getParameter("flag");
        request.setCharacterEncoding("UTF-8");
        if("login".equals(flag)) {
            login(request,response);
        }
        else if("checkreg".equals(flag)) {
            checkreg(request,response);
        }
        else if("add".equals(flag)) {
            add(request,response);
        }
        else if("manager".equals(flag)){
            manager(request,response);
        }
        else if("delet".equals(flag)){
            delet(request,response);
        }
        else if("suo".equals(flag)){
            suo(request,response);
        }
        else if("jie".equals(flag)){
            jie(request,response);
        }
        else if("update".equals(flag)){
            update(request,response);
        }
        else if("updatesub".equals(flag)){
            updatesub(request,response);
        }else if("duoshan".equals(flag)){
            duoshan(request,response);
        } else if ("logout".equals(flag)) {
            logout(request,response);
        }else if("checkpassword".equals(flag)){
            checkpassword(request,response);
        }else if("uppsw".equals(flag)){
            uppsw(request,response);
        }else if ("flush".equals(flag)){
            flush(request,response);
        }
    }

    private void flush(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AdminInfo adminInfo = (AdminInfo) request.getAttribute("session_admin");
        request.setAttribute("session_admin",adminInfo);
        response.getWriter().print("zzz");
    }

    private void uppsw(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String adminName=request.getParameter("adminName");
        String psw=request.getParameter("password");
        Des des=new Des();
        psw=des.encStr(psw);
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAdminName(adminName);
        adminInfo.setPassword(psw);
        int i=adminDao.updatepsw(adminInfo);
        request.setAttribute("uppsw","uppsw");
        logout(request,response);
    }

    private void checkpassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String psw=request.getParameter("password");
        Des des=new Des();
        String pas=des.encStr(psw);
        AdminInfo adminInfo=adminDao.checkpsw(pas);

        if(adminInfo!=null) {
            String password = des.desStr(adminInfo.getPassword());
            response.getWriter().print("1");
        }else {
            response.getWriter().print("0");
        }

    }

    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String p= request.getParameter("uppsw");
        if("uppsw".equals(p)){
            request.getSession().invalidate();
            response.getWriter().print("<script>" +
                    "alert('修改成功，请重新登录');" +
                    "window.top.location.href='login.jsp' </script>");
        }
        request.getSession().invalidate();
        //request.getRequestDispatcher("/login.html").forward(request, response);  这样不行,目标页面会在框架集中弹出
        response.getWriter().print("<script>window.top.location.href='login.jsp' </script>");
    }

    private void duoshan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idlist=request.getParameter("id");
        String sid[]= idlist.split(" ");
        for(int i=0;i<sid.length;i++){
            adminDao.del(Integer.parseInt(sid[i]));
            System.out.println(sid[i]);
        }
        manager(request,response);
    }


    private void updatesub(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String adminName=request.getParameter("adminName");
        String note=request.getParameter("note");
        AdminInfo adminInfo=new AdminInfo();
        adminInfo.setId(id);
        adminInfo.setAdminName(adminName);
        adminInfo.setNote(note);

        int n=adminDao.update(adminInfo);
        if(n==1)
            request.setAttribute("msg","修改成功");
        request.setAttribute("id",id);
        request.setAttribute("adminName",adminName);
        request.setAttribute("note",note);
        request.getRequestDispatcher("admin/admin_update.jsp").forward(request, response);

    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String adminName=request.getParameter("adminName");
        String note=request.getParameter("note");
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setId(id);
        adminInfo.setAdminName(adminName);
        adminInfo.setNote(note);
        int n=adminDao.update(adminInfo);
        if(n>0) {
            request.setAttribute("id", id);
            request.setAttribute("adminName", adminName);
            request.setAttribute("note", note);
            request.getRequestDispatcher("admin/admin_update.jsp").forward(request, response);
        }
    }

    private void jie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        adminDao.jie(id);
        manager(request,response);
    }

    private void suo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        adminDao.suo(id);
        manager(request,response);
    }

    private void delet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        adminDao.del(id);
        manager(request,response);
    }

    private void manager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageIndex=1;
        int pageSize=6;
        int rowCount=adminDao.getAdminCount();

        String pageIndexStr=request.getParameter("pageIndex");
        if(pageIndexStr!=null) {
            pageIndex=Integer.parseInt(pageIndexStr);
        }
        PageInfo page= PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<AdminInfo> adminList= adminDao.getAdminList(page);
        request.setAttribute("adminList", adminList);
        request.setAttribute("page", page);
        request.getRequestDispatcher("admin/admin_manager.jsp").forward(request, response);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminName=request.getParameter("adminName");
        String password=request.getParameter("password");
        Des des=new Des();
        password=des.encStr(password);
        String note=request.getParameter("note");
        AdminInfo admin=new AdminInfo();
        admin.setAdminName(adminName);
        admin.setPassword(password);
        admin.setNote(note);
        admin.setState("1");
        int result=adminDao.addAdmin(admin);
        if(result!=0) {
            request.setAttribute("msg", "用户添加成功");
            request.getRequestDispatcher("admin/admin_add.jsp").forward(request, response);
        }
    }

    private void checkreg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name= (String)request.getParameter("username");
        AdminInfo adminInfo= adminDao.getAdminNamebyid(name);
        if(adminInfo==null)
            response.getWriter().print(0);
        else
            response.getWriter().print(1);
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String adminName=request.getParameter("adminName");
        String password=request.getParameter("password");
        Des des=new Des();
        password=des.encStr(password);
        System.out.println(password);
        AdminInfo admin=adminDao.login(adminName, password);
        if(admin!=null) {
            if(admin.getState().equals("2")) {
                response.getWriter().print("2");
            }
            else {
                //把当前用户的信息放到session中
                request.getSession().setAttribute("session_admin", admin);
                response.getWriter().print("1");
            }
        }
        else {
            response.getWriter().print("0");
        }
    }


}
